package com.tkx.dxb.service.article.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkx.dxb.common.base.result.R;
import com.tkx.dxb.service.article.entity.Post;
import com.tkx.dxb.service.article.entity.vo.PostQueryVo;
import com.tkx.dxb.service.article.service.PostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author victor
 * @since 2021-04-07
 */
@Api(tags = "文章")
@RestController
@RequestMapping("/admin/article/post")
public class PostController {

    private PostService postService;

    @Autowired
    public void setPostService(PostService postService){
        this.postService = postService;
    }

    @ApiOperation("所有文章列表")
    @GetMapping("list")
    public R listAll(){
        List<Post> list = postService.list();
        return R.ok().data("list", list);
    }

    @ApiOperation("新增文章")
    @PostMapping("save")
    public R save(@ApiParam(value = "文章对象", required = true) @RequestBody Post post){
        boolean result = postService.save(post);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID删除文章",notes = "根据ID删除文章，逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "文章ID",required = true) @PathVariable String id) {
        // 删除封面
        postService.removeAvatarById(id);
        // 删除文章
        boolean result = postService.removeById(id);
        if (result) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据ID列表删除文章")
    @DeleteMapping("batch-remove")
    public R removeRows(
            @ApiParam(value = "文章ID列表",required = true)
            @RequestBody List<String> idList) {
        // 删除列表图片
        postService.removeAvatarByIds(idList);
        // 删除列表文章
        boolean result = postService.removeByIds(idList);
        if (result) {
            return R.ok().message("删除成功！");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("更新文章")
    @PutMapping("update")
    public R updateById(@ApiParam("文章对象") @RequestBody Post post){
        boolean result = postService.updateById(post);
        if(result){
            return R.ok().message("修改成功");
        }else{
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("回显文章信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam("文章回显ID") @PathVariable String id){
        Post post = postService.getById(id);
        if (post != null){
            return R.ok().data("item", post);
        }else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("文章分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(
            @ApiParam(value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
            @ApiParam("文章列表查询对象") PostQueryVo postQueryVo) {
        Page<Post> pageParam = new Page<>(page, limit);
        /**
         * queryWrapper 查询条件，无查询对象省略
         */
        IPage<Post> pageModel = postService.selectPage(pageParam, postQueryVo);
        /**
         * getRecords 获取数据
         * getTotal   获取总页数
         */
        List<Post> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
}

